#!/usr/bin/python
# -*- coding : UTF-8 -*-
# ========================
# @Time  : 2020/5/15 9:55
# @Author: Mecell
# @File  : context_packaging.py
# ========================

import re
import json

from data.com_function import day_after, generate_random_no
from common.yaml_packaging import YamlHandle
from common.path_constants import LOGIN_YAML,USER_ACCOUNT_CONF,RE_CONF
from common.mysql_packaging import MysqlHandle
from common.config_packaging import ConfigHandle



class ContextHandle:
    """
    定义一个实现参数化，反射功能的类
    """
    user_config = ConfigHandle(USER_ACCOUNT_CONF)
    re_config = ConfigHandle(RE_CONF)

    mysql_obj = MysqlHandle()
    emp_info = mysql_obj.get_avail_emp_info()
    mysql_obj.close_sql()

    @classmethod
    def rep_cur_emp_num(cls, data):
        """
        替换可用员工的工号
        :param emp_num:
        :return:
        """
        cur_emp_num_pattern = re.compile(eval(cls.re_config("employee","cur_emp_num")))
        if re.search(cur_emp_num_pattern, data):
            rep_emp_num = cls.emp_info[0]
            data = re.sub(cur_emp_num_pattern, rep_emp_num, data)
        cur_emp_depart_pattern = re.compile(eval(cls.re_config("employee", "cur_emp_depart")))
        if re.search(cur_emp_depart_pattern, data):
            rep_emp_depart = cls.emp_info[3]
            data = re.sub(cur_emp_depart_pattern, rep_emp_depart, data)
        return data

    @classmethod
    def rep_cur_emp_name(cls, data):
        """
        替换可用员工的姓名
        :param data:
        :return:
        """
        cur_emp_name_pattern = re.compile(eval(cls.re_config("employee","cur_emp_name")))
        if re.search(cur_emp_name_pattern, data):
            rep_emp_name = cls.emp_info[1]
            data = re.sub(cur_emp_name_pattern, rep_emp_name, data)
        return data

    @classmethod
    def rep_cur_user_id(cls, data):
        """
        替换可用员工的用户id
        :param data:
        :return:
        """
        cur_user_id_pattern = re.compile(eval(cls.re_config("employee", "cur_user_id")))
        if re.search(cur_user_id_pattern, data):
            rep_user_id = str(cls.emp_info[2])
            data = re.sub(cur_user_id_pattern, rep_user_id, data)
        return data

    @classmethod
    def rep_cur_user_depart(cls, data):
        """
        替换可用员工的部门名称
        :param data:
        :return:
        """
        cur_user_depart_pattern = re.compile(eval(cls.re_config("employee", "cur_user_depart")))
        if re.search(cur_user_depart_pattern, data):
            rep_user_depart = str(cls.emp_info[3])
            data = re.sub(cur_user_depart_pattern, rep_user_depart, data)
        return data

    @classmethod
    def rep_err_emp_num(cls, data):
        """
        替换不存在的员工工号
        :param emp_num:
        :return:
        """
        err_emp_num_pattern = re.compile(eval(cls.re_config("employee","err_emp_num")))
        cls.mysql_obj = MysqlHandle()
        if re.search(err_emp_num_pattern, data):
            rep_emp_num = cls.mysql_obj.generate_unavail_emp_num()
            data = re.sub(err_emp_num_pattern, rep_emp_num, data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def rep_lock_emp_num(cls, data):
        """
        替换已锁定的员工工号
        :param emp_num:
        :return:
        """
        lock_emp_num_pattern = re.compile(eval(cls.re_config("employee","lock_emp_num")))
        cls.mysql_obj = MysqlHandle()
        if re.search(lock_emp_num_pattern, data):
            rep_emp_num = cls.mysql_obj.generate_lock_emp_num()
            data = re.sub(lock_emp_num_pattern, rep_emp_num, data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def rep_day_after(cls, data, n):
        """
        替换日期参数
        :param data:
        :param n:
        :return:
        """
        day_after_pattern = re.compile(eval(cls.re_config("day", "day_after")))
        if re.search(day_after_pattern, data):
            rep_day_after = day_after(n)
            data = re.sub(day_after_pattern, rep_day_after, data)
        return data

    @classmethod
    def rep_uuid(cls, data, prefix="159", n=10):
        """
        替换文件uuid
        :param data:
        :return:
        """
        uuid_pattern = re.compile(eval(cls.re_config("uuid", "uuid")))
        if re.search(uuid_pattern, data):
            rep_uuid = prefix + generate_random_no(n)
            data = re.sub(uuid_pattern, rep_uuid, data)
        return data

    @classmethod
    def login_parameterize(cls, data):
        """
        登录数据参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = cls.rep_cur_emp_num(data)
        data = cls.rep_cur_emp_name(data)
        data = cls.rep_cur_user_id(data)
        data = cls.rep_err_emp_num(data)
        data = cls.rep_lock_emp_num(data)
        return data


if __name__ == '__main__':
    yaml_obj = YamlHandle(LOGIN_YAML)
    yaml_data = yaml_obj.get_data('login_success')
    data_before = yaml_data[0]
    data_after = ContextHandle.login_parameterize(data_before)
    print(f"参数化前：{data_before}\n参数化后：{data_after}")


